package cn.liyao.web.repository;

import cn.liyao.web.model.Group;
import cn.liyao.web.model.Messages;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;


import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Date;
import java.util.List;

public interface MessageRepository extends JpaRepository<Messages, String> {


    @Query("select m from UserMessage tum JOIN Messages m on m.fp = tum.fp  where ((m.from = :sender and m.to = :to ) or  (m.from = :to and m.to = :sender )) and (m.timestamp < :timestamp and tum.deletedStatus = 0 and tum.targetId = :targetId) order by  m.timestamp desc")
    Slice<Messages> findMessageListPrivate(@Param("sender") String sender,@Param("to") String to,@Param("timestamp") Long timestamp,@Param("targetId") String targetId,  Pageable pageable);


//    @Query("select m from Messages m    where ((m.from = :sender and m.to = :to ) or  (m.from = :to and m.to = :sender )) and (m.timestamp < :timestamp) order by  m.timestamp desc  ")
//    Slice<Messages> findMessageList(@Param("sender") String sender,@Param("to") String to,@Param("timestamp") Long timestamp, Pageable pageable);



    @Query("select m from UserMessage tum JOIN Messages m on m.fp = tum.fp  where m.to = :to  and (m.timestamp < :timestamp and tum.deletedStatus = 0 and tum.targetId = :targetId and tum.userId = :userId) order by  m.timestamp desc  ")
    Slice<Messages> findGroupMessageList(@Param("to") String to, @Param("timestamp") Long timestamp,@Param("targetId") String targetId,@Param("userId") String userId, Pageable pageable);


//    @Query("select m from Messages m where m.to = :to  and (m.timestamp < :timestamp) order by  m.timestamp desc  ")
//    Slice<Messages> findGroupMessageList(@Param("to") String to, @Param("timestamp") Long timestamp, Pageable pageable);


}